|
|
|
|
|
|
|
Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete And List1.ListIndex <> -1 Then
List1.RemoveItem List1.ListIndex
End If
End Sub |
|
|
|
|
|
|
|
|
Run the program with the new modification, and you should be able to remove a specific item from the list by selecting it and pressing the Delete key. |
|
|
|
|
|
|
|
|
You already know how to use the AddItem method to add an item to the end of the list. An optional parameter, Index, can be used with the AddItem method. This parameter specifies the location within the list where you want the new item to appear. You specify the index value of the item in front of which you want to add your item. For example, if the list contains five items, and you want your item to appear ahead of the third item, you use code like the following: |
|
|
|
|
|
|
|
|
lstAvailable.AddItem "Corvette", 2 |
|
|
|
|
|
|
|
|
In the preceding section, you saw how to modify the list by adding and removing items. You also saw that you can add an item to a specific location by specifying an index. But what do you do if you want your entire list sorted in alphabetic order? |
|
|
|
|
|
|
|
|
Fortunately, this task is simple. To sort the list, you simply set the Sorted property to True. Then, no matter in which order you enter your items, they appear in alphabetic order to the users. The indexes of the list items are adjusted as they are added so that they remain in order. |
|
|
|
|
|
|
|
|
|
Remember, in alphabetic order, 2 is listed after 11.  |
|
|
|
|
|
|
|
|
|
|
This built-in sorting capability is a nice feature of the list box because you can use it to sort data quickly. However, you have to specify the Sorted property at designtime. If you want to have the users turn sorting on or off at runtime, you can use two list box controls and use the Visible properties so that only one is displayed at a time. |
|
|
|
|
|
|
|
|
Setting the Appearance of a List Box |
|
|
|
|
|
|
|
|
In addition to the ''plain" list box in Figure 4.9, several additional styles are available. You can make your list look like a series of check boxes or include multiple columns of items. |
|
|
|
|
|
|
|
|
You handle the selection of the list type by setting the Style property. The two settings of the property are 0 - Standard and 1 - Checkbox, as illustrated in Figure 4.10. You also can change this property at runtime by setting its value to one of the intrinsic constants vbListBoxStandard or vbListBoxCheckBox, respectively. |
|
|
|
|
|
|
|
|
Another way to change the list's appearance is to use the Columns property. The default value of the property is 0. This value results in the standard list box previously discussed. Setting the property to 1 causes the list to be presented one column at a time, but to scroll horizontally instead of vertically. Setting the property to greater than 1 causes the list to display in the number of columns specified by the property (for example, a value of 2 displays the list in two col- |
|
|
|
|
|